const fs = require("fs");
const path = require("path");

const copyModule = require('./copyModule')

// 搬移模板

  // 根目录
  let root_path = path.resolve('.')
  const fishConfig = require(path.join(root_path,'/fishConfig/fish_config'));
  // const fishConfig = require(path.join(root_path,'/lib/config/fish_config'));
  const fishParams = require(path.join(root_path,'/fishConfig/fish_params'));
  // const fishParams = require(path.join(root_path,'/lib/config/fish_params'));

  const toTouchFish = (params) => {
    
    // 目标文件夹目录 模板文件目录 模板类型需要可配置 通过传参 可将模板文件夹相互嵌套
    
    // 目标文件夹目录
    let outFilterPath = root_path+  fishConfig.OUT_FILTER_PATH;
    // 模板文件夹目录
    let modulePath = root_path + fishConfig.MODULE_PATH;
    
    let moduleType = fishConfig.MODULE_TYPE;
    

    // 判断模板URL是否可用
    let moduleUrlEffective = false;
    let _modulePath = modulePath;
    let _outPath = path.join(outFilterPath, params.path);
    console.log("_modulePath =====>", _modulePath);
    console.log("_outPath =====>", _outPath);
    try {
      fs.accessSync(_modulePath);
      moduleUrlEffective = true;
      console.log("模板URL可读可写");
    } catch (err) {
      moduleUrlEffective = false;
      console.error("模板URL不可访问");
    }
    console.log("moduleUrlEffective ======>", moduleUrlEffective);
    let moduleFilePath = path.resolve(
      path.join(_modulePath, moduleType[params.type])
    );
    console.log("moduleFilePath =====>", moduleFilePath);
    if (!fs.statSync(moduleFilePath).isDirectory()) {
      throw "模板文件路径有误";
    }
    // 目标文件目录
    let outFile = path.resolve(path.join(_outPath, params.name));
    console.log("outFile =====>", outFile);
    if (params.name) {
      //创建文件夹
      if (!fs.existsSync(outFile)) {
        fs.mkdirSync(outFile);
        console.log("创建");
      }
    }
    

    // 根据param的type先创建底层的模板
    copyModule(moduleFilePath, outFile,params.targetArr);

    // 嵌套创建
    if (params.nest) {
      params.nest.forEach((item) => {
        let _folder = item.path.split("/");
        _folder.shift();
        console.log("_folder ====>", _folder);
        let _param = [];

        _folder.forEach((f, index) => {
          let _type, _f;
          let m_path;
          if (f.indexOf("{") > -1) {
            _f = f.slice(f.indexOf("}") + 1, f.length);
            _type = f.slice(1, f.indexOf("}"));
          } else if (moduleType[f]) {
            _f = f;
            _type = f;
          } else {
            _f = f;
          }
          console.log("_f ====>", _f);
          console.log("_type ====>", _type);

          if (_type) {
            m_path = moduleType[_type];
          }
          _param.push({
            out_path: _f,
            module_path: m_path,
          });
        });
        console.log("_param ====>", _param);
        let _out_path = outFile;
        _param.forEach((e, idx) => {
          _out_path = path.join(_out_path, e.out_path);
          //创建文件夹
          if (!fs.existsSync(_out_path)) {
            fs.mkdirSync(_out_path);
            console.log("创建" + e.out_path);
            if (e.module_path) {
              copyModule(
                path.resolve(path.join(_modulePath, e.module_path)),
                _out_path,
                item.targetArr
              );
            }
          }
        });
      });
    }
  };

  toTouchFish(fishParams);
